package cn.javabb.admin.controller.sys;

import cn.javabb.common.exception.ParameterException;
import cn.javabb.common.model.ResponseModel;
import cn.javabb.common.utils.ListUtils;
import cn.javabb.core.base.BaseController;
import cn.javabb.core.entity.sys.LogDO;
import cn.javabb.core.query.LogQuery;
import cn.javabb.core.service.sys.LogService;
import cn.javabb.log.action.DeleteAction;
import cn.javabb.log.annotation.Log;
import com.github.pagehelper.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author QINB imqinbao@163.com
 * @CreateDate 2018/11/5/005 17:39
 * @Since V1.0
 */
@Controller
@RequestMapping("/admin/log")
public class LogAdminController extends BaseController {

    @Autowired
    private LogService logService;

    /**
     * 日志列表页面
     * @author QINB
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String toListPage(Model model) {
        return "admin/system/log";
    }
    /**
     * 查询所有登录日志
     **/
    @RequiresPermissions("log:view")
    @ResponseBody
    @GetMapping("/")
    public Object list(LogQuery query) {
        Page<?> p = logService.list(query);
        return ResponseModel.table(p);
    }

    /**
     * 日志删除
     * @param ids
     * @return
     */
    @RequiresPermissions("log:del")
    @ResponseBody
    @DeleteMapping("/{ids}")
    @Log(name = "日志删除",action = DeleteAction.class)
    public Object delBatch(@PathVariable(value = "ids") String ids){
        if (StringUtils.isEmpty(ids)) {
            throw new ParameterException();
        }
       int num = logService.deleteByIds(LogDO.class,"id", ListUtils.stringToListString(ids));
        if (num > 0) {
            return ResponseModel.Success("删除成功");
        }
        return ResponseModel.Failure("删除失败");
    }

    /**
     * 清空日志
     * @return
     */
    @RequiresPermissions("log:del")
    @ResponseBody
    @DeleteMapping("/clearAll")
    @Log(key=DeleteAction.LOG_DELETE_CLEAR,action = DeleteAction.class)
    public Object clearAll(){
        int num = logService.deleteByWhere(null);
        if (num > 0) {
            return ResponseModel.Success("清空成功");
        }
        return ResponseModel.Failure("清空失败");
    }
}
